#ifndef BRUTEFORCECLASS_H
#define BRUTEFORCECLASS_H

#include <QThread>
#include "GenCrack.h"
#include "MosaicPlugin/MosaicPlugin/PluginTemplate.h"
#include "SlaveSlot.h"
#include "ui_mainwindowcrack.h"


class MainWindowCrack;


typedef struct TInfoBruteForce {
    unsigned char           *m_prefix;
    unsigned char           *m_postfix;
    char                    *m_dictionary_file;
    unsigned short          m_min_prefix;
    unsigned short          m_max_prefix;
    unsigned short          m_min_postfix;
    unsigned short          m_max_postfix;
    unsigned short          m_numThread;
    TCombinatorio           *thiss;

    TInfoBruteForce() : m_prefix(0), m_postfix(0), m_dictionary_file(0), m_min_prefix(0), m_max_prefix(0), m_min_postfix(0), m_max_postfix(0), m_numThread(0), thiss(0) {}
} TInfoBruteForce;

//
// intermediate class to use as bridge between program and plugin:
class BruteForceClass : public QThread, public TCombinatorio {
    private:
        PluginInterface         *m_extClass;
        Ui::MainWindowCrack     *m_guiMainCrack;
        MainWindowCrack         *m_mainCrack;
        QVector<SlaveSlot *>    m_slaveList;
        bool                    m_gridElaborationInProgress; // indicates when elaboration is active
        __int64                 m_unitsInMasterQueue;
        //
        // parameters for grid elaboration:
        TInfoBruteForce         m_infoParams;

        BruteForceClass(BruteForceClass&);

    public:
        BruteForceClass(MainWindowCrack *mainCrack, Ui::MainWindowCrack *guiMainCrack, QVector<SlaveSlot *>& slaveList, PluginInterface *plugin) : TCombinatorio(0, 0, 0, true),
            m_extClass(plugin), m_guiMainCrack(guiMainCrack), m_mainCrack(mainCrack), m_slaveList(slaveList), m_gridElaborationInProgress(false), m_unitsInMasterQueue(0) {}
        virtual ~BruteForceClass();
        //
        // to set parameters for grid computing:
        void setParamForGridComputing(unsigned char *prefix, unsigned short min_prefix, unsigned short max_prefix, unsigned char *postfix, unsigned short min_postfix, unsigned short max_postfix, char *dictionary_file, unsigned short numThread);

    protected:
        void run(void);

        virtual void callback_start_cleaner(void);
        virtual void callback_end_cleaner(void);
        //
        // funzioni di callback per la gestione dell'elaborazione distribuita (contesto Master):
        virtual void callback_query_voluntary_accept(TSocketInfo *voluntary, bool *isAccepted);
        virtual void callback_accept_voluntary(const TSocketInfo *voluntary);
        virtual void callback_send_work_unit(const TSocketInfo *slave, const unsigned char *combinazione, unsigned short len_array);
        virtual void callback_work_unit_complete(const TSocketInfo *slave, __int64 total);
        //
        // funzioni di callback per la gestione dell'elaborazione distribuita (contesto Slave):
        virtual void callback_reject_voluntary(const TSocketInfo *master);
        virtual void callback_voluntary_confirmed(const TSocketInfo *master);
        virtual void callback_receive_work_unit(const TSocketInfo *master, const unsigned char *combinazione, unsigned short len_array);
        virtual void callback_abort_listner(void);
        //
        // funzioni comuni per la generazione:
        virtual void callback_new_combination(const_lista combinazione, unsigned short len_array);
        virtual void callback_partial_generate_bruteforce(const char *word);
        virtual void callback_start_generate_bruteforce(void);
        virtual void callback_end_generate_bruteforce(void);

private slots:
    void on_cmdAttivaMaster_clicked();
    void on_cmdBruteForceSetting_clicked();
};

#endif // BRUTEFORCECLASS_H
